<?php namespace app\home\controller;
use houdunwang\view\View;
use system\model\Category;
use system\model\Links;
use system\model\System;
use system\model\Tag;
class Common{
    //获得公用部分的数据
    public function __construct(){
        //找出最新文章
        $news=Db::table('articledata')->orderBy('pubtime','DESC')->limit(2)->get(['aid','title','pubtime']);
//        p($news);
        View::with('news',$news);

        //获得配置数据
        $data=(new System())->getdata();
        View::with('data',$data);
//        p($data);
        //获得顶级分类
        $category=(new Category())->getdata();
        View::with('category',$category);
        //获得友情链接
        $links=(new Links())->getlinks(2);
        View::with('links',$links);
        //获得标签
        $tags=(new Tag())->index(5);
        View::with('tags',$tags);
//        标签云
        $tagss=(new Tag())->get()->toArray();
//        找出每个标签拥有多少篇文张
        foreach($tagss as $k=>$v){
            $tagss[$k]['arcs']=Db::table('article_tag')->where('tid',$v['tid'])->count();
        }
        View::with('tagss',$tagss);
//        p($tagss);
//        分类列表，及其该分类拥有的文章
        $cnames= Db::table('category')->get(['cid','cname']);
        foreach($cnames as $k=>$v){
            $cnames[$k]['arcs']=Db::table('article')->where('cid',$v['cid'])->count();
        }
        View::with('cnames',$cnames);
//        p($cnames);
    }

}
